﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MISAPP.Classify.DataAccess;
using System.Data;
using MISAPP.MISInterface;

namespace MISAPP.Classify.Service
{
    public class Service_WMG002
    {
        public enum Category
        { 存款類別 = 1, 存期別, 客群別, 群組 }

        DA_WMG002 da = null;
        protected DA_WMG002 DA
        {
            get
            {
                if (da == null)
                {
                    da = new DA_WMG002();
                }
                return da;
            }
        }
        protected DataTable dt = null;


        public DataSet GetCriteriaData()
        {
            return DA.GetCriteria();
        }

        public string[] GetCriteriaArray()
        {
            string[] flds = DA.GetDistinctField();
            System.Collections.ArrayList newFld = new System.Collections.ArrayList();
            foreach (string str in flds)
            {
                newFld.Add(str);
            }
            newFld.Add("Category");
            return (string[])newFld.ToArray(typeof(string));
        }

        public DataTable GetSummaryData(System.Collections.ArrayList p_criteria)
        {
            
            //if (p_criteria.Count != 1)
            //{
            //   throw new Exception("參數傳遞錯誤!");
            //}
            try
            {
                string[] option = {string.Empty};
                List<iSelectList> criteriaList = p_criteria[0] as List<iSelectList>;
                SelectList SL = criteriaList[0] as SelectList;
                string[] yms = SL.GetListArray();
                if (criteriaList.Count.Equals(2))
                {
                    SelectList SL2 = criteriaList[1] as SelectList;
                    option = SL2.GetListArray();
                }

                dt = DA.GetSummaryData(yms[0],option[0]);
              
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

          
            return dt;
        }

        protected void cal(string[] p_category,int p_i)
        {
            string tot_bal = "TOT_AVG_BAL";
            double total,result;
            total = 0.0;
            result = 0.0;
            System.Data.DataRow row = null;
            foreach (string str in p_category)
            {
                //row = ;
                total = Convert.ToDouble(dt.Rows[p_i][tot_bal].ToString());
                p_i++;
                result = Math.Round( Convert.ToDouble(
                    dt.Rows[p_i][tot_bal].ToString()) / total * 100 ,2);
                try
                {
                    dt.Rows[p_i]["存款類別"] = string.Format("{0}%", result.ToString());
                }
                catch (Exception ex)
                { 
                
                }
                
                p_i++;
                result = Math.Round(Convert.ToDouble(
                    dt.Rows[p_i][tot_bal].ToString()) / total * 100, 2);
                dt.Rows[p_i]["存款類別"] = string.Format("{0}%", result.ToString());
            }
        }
    }
}
